<?php
class BaseModelSet extends BaseModelObject {
    /**
     * create dir recursive
     * @param string $pathname
     * @param string $mode
     * @return bool
     */
	public static function recursiveMkdir($pathname, $mode=0775) {		
		is_dir(dirname($pathname)) || self::recursiveMkdir(dirname($pathname), $mode);
		return is_dir($pathname) || mkdir($pathname, $mode);
	}
	/**
	 * mixed array recursive implode to string
	 * @param string $glue
	 * @param mixed array $pieces
	 * @return string
	 */	
	public static function recursiveImplode($glue, $pieces) {
		foreach( $pieces as $r_pieces ) {
			if( is_array($r_pieces) ){
			  	$retVal[] = self::recursiveImplode( $glue, $r_pieces );
			}
			else{
			  	$retVal[] = $r_pieces;
			}
		}
		return @implode($glue, $retVal);
	}
	/**
	 * get user client ip address
	 * @return string
	 */
	public static function getIP() {
		if ($HTTP_SERVER_VARS['HTTP_X_FORWARDED_FOR']) {
			$_tmp = split(",",$HTTP_SERVER_VARS['HTTP_X_FORWARDED_FOR']);
			return $_tmp[0];
		}
		else if ($HTTP_SERVER_VARS['HTTP_CLIENT_IP']) {
			return $HTTP_SERVER_VARS['HTTP_CLIENT_IP'];
		}
		else if ($HTTP_SERVER_VARS['REMOTE_ADDR']) {
			return $HTTP_SERVER_VARS['REMOTE_ADDR'];
		}
		else if (getenv('HTTP_X_FORWARDED_FOR')) {
			$_tmp = split(",",getenv('HTTP_X_FORWARDED_FOR'));
			return $_tmp[0];
		}
		else if (getenv('HTTP_CLIENT_IP')){
			return getenv('HTTP_CLIENT_IP');	
		}
		else if (getenv('REMOTE_ADDR')) {
			return getenv('REMOTE_ADDR');
		}
		else {
			return '0.0.0.0';
		}
	}
	/**
	 * check the string is ip address or not
	 * @param string $ip
	 * @return bool
	 */
    public static function isIP($ip) {
		$_pattern = array(
			'ip' => '(?:(?:25[0-5]|2[0-4][0-9]|(?:(?:1[0-9])?|[1-9]?)[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|(?:(?:1[0-9])?|[1-9]?)[0-9])',
			'hostname' => '(?:[a-z0-9][-a-z0-9]*\.)*(?:[a-z0-9][-a-z0-9]{0,62})\.(?:(?:[a-z]{2}\.)?[a-z]{2,4}|museum|travel)'
		);
		if (preg_match('/^' . $_pattern['ip'] . '$/',$ip)) {
			return true;
		} 
		else {
			return false;
		}
	}
	public static function jsonFormat($data, $msg = '') {
		//header("Content-Type: application/json; charset=utf-8");
		$ret = array('status' => 0, 'msg' => '');
		if (empty($data)) {
			$ret['status'] = 1;
		}
		$ret['msg'] = $msg;
		$ret['data'] = empty($data) ? array() : $data;
		return json_encode($ret);
	}
	
	public static function xmlFormat($data, $msg = '') {
		header("Content-Type: text/xml; charset=utf-8");
		$ret = '<?xml version="1.0" encoding="utf-8"?>';
		$ret .= '<root>';
		if (empty($data)) {
			$ret .= '<status>1</status>';
			$ret .= '<msg>'.$msg.'</msg>';
			$ret .= '</root>';
			return $ret;
		}
		$ret .= '<status>0</status>';
		$ret .= '<msg>'.$msg.'</msg>';
		if (is_array($data) && !empty($data)) {
			foreach ($data as $key => $val) {
				if (is_array($val) && !empty($val)) {
					$ret .= "<data>";
					foreach ($val as $k => $v) {
						if (is_numeric($k)) {
							continue;
						}
						$ret .= "<$k>$v</$k>";
					}
					$ret .= "</data>";
				}
				else {
					if (is_numeric($key)) {
						continue;
					}
					$ret .= "<$key>$val</$key>";
				}
			}
		}
		else {
			$ret .= '<data>'.$data.'</data>';
		}
		$ret .= '</root>';
		return $ret;
	}
	
	public static function format($type, $data, $msg = '') {
		if (!in_array($type, array('xml', 'json'))) {
			$type = 'json';
		}
		return call_user_func_array(array(__CLASS__, "{$type}Format"), array($data, $msg));
	}
	
	public static function getEmailPrefix($uname) {
		if (($pos = strpos($uname, "@"))!=false) {
			$uname = substr($uname, 0, $pos);
		}
		return $uname;
	}
	
	public static function validateEmail($str) {
		return preg_match('/^[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)*@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+){1,4}$/', $str) ? true : false;
	}
	
	public static function validateURI($uri) {
		$allow = array('http', 'https', 'ftp');        
        if (preg_match('!^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?!', $uri, $matchs)) {
            $scheme = $matches[2];
            if (in_array($scheme, $allow)) {
                return true;
            }
        }
        return false;
	}

	public static function validatePhone($phone) {
		$mode = "/^1(3[0-9]|5[012356789]|8[02356789]|4[57])\d{8}$/";
		if (preg_match($mode, $phone) == 1) {
			return true;
		}
		return false;
	}
	
	public static function validateIDNO($idno) {
		if (preg_match("/^\d{15,18}(x{0,1})$/i", $idno) == 1) {
			return true;
		}
		return false;
	}
	public static function getDateDistance($date1, $date2) {
  		$d1 = strtotime($date1);
  		$d2 = strtotime($date2);
  		$dis = round(abs($d1 - $d2) / (24 * 60 * 60));
  		return $dis;
	} 
	
	public static function isFloatEqual($f1, $f2){
		$temp1 = $f1 + 0.001;
		$temp2 = $f1 - 0.001;
		if($temp1 > $f2 && $temp2 < $f2){
			return true;
		}
		return false;
	}
	
	public static function strEmpty($str) {
		if (strlen($str)==0) {
			return true;
		}
		return false;
	}
	
	public static function flitSpace($string) {
		$str = trim($string);
		$str = strip_tags($str);
		$str = str_replace("\r\n","",$str);
		$str = preg_replace("{\t}","",$str);  
		$str = preg_replace("{\r}","",$str);  
		$str = preg_replace("{\n}","",$str);
		return $str;		
	}
}
?>